import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class nettyClient {
    private static ChannelFuture f;
    static EventLoopGroup workerGroup;
    static Bootstrap b;
    static ChannelFutureListener channelFutureListener=null;
    static NettyClientHandlerInner nettyClientHandlerInner = new NettyClientHandlerInner();

    public static void main(String[] args) throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(2000);
                        nettyClientHandlerInner.sendMSG("writeWumingStatus@@" + new Random().nextInt(20000));
                        Thread.sleep(2000);
                        nettyClientHandlerInner.sendMSG("writePazhanfoStatus@@" + new Random().nextInt(20000));

                        nettyClientHandlerInner.sendMSG("pkRecord@@" + new Random().nextInt(20000));

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        init();
        connectToServer(nettyClientHandlerInner);
    }

    public static void init() {
        workerGroup = new NioEventLoopGroup();
        b = new Bootstrap(); // (1)
        b.group(workerGroup); // (2)
        b.channel(NioSocketChannel.class); // (3)
        b.option(ChannelOption.SO_KEEPALIVE, true); // (4)
        b.handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
                ch.pipeline().addLast(new StringDecoder());
                ch.pipeline().addLast(nettyClientHandlerInner);
            }
        });

        channelFutureListener=new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if(!channelFuture.isSuccess()){
                    channelFuture.channel().eventLoop().schedule(new Runnable() {
                        @Override
                        public void run() {
                           doReconnect();
                        }
                    },3,TimeUnit.SECONDS);
                }else{
                    System.out.println("重连成功");
                }
            }
        };
    }

    public static void connectToServer(NettyClientHandlerInner nettyClientHandler) {
        try {
            // Start the client.
            f = b.connect("127.0.0.1", 10004).sync(); // (5)
            f.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    if(!channelFuture.isSuccess()){
                        System.out.println("重试错误");
                        channelFuture.channel().eventLoop().schedule(new Runnable() {
                            @Override
                            public void run() {
                                doReconnect();
                            }
                        },3,TimeUnit.SECONDS);
                    }else{
                        System.out.println("重试成功");
                    }
                }
            });
            // Wait until the connection is closed.
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void doReconnect(){
        ChannelFuture future=b.connect("127.0.0.1", 10004);
        future.addListener(channelFutureListener);
    }
}